<template>
  <el-card>
    <div style="margin-bottom: 16px; display: flex; gap: 16px; align-items: center">
      <el-input
        v-model="queryParams.spaceName"
        placeholder="空间名称"
        clearable
        style="width: 200px"
        @keyup.enter="handleSearch"
      />
      <el-select
        v-model="queryParams.spaceLevel"
        placeholder="空间级别"
        clearable
        style="width: 160px"
        @change="handleSearch"
      >
        <el-option
          v-for="level in spaceLevels"
          :key="level.value"
          :label="level.text"
          :value="level.value"
        />
      </el-select>
      <el-button type="primary" @click="handleSearch">查询</el-button>
      <el-button type="success" @click="openAddDialog">新增空间</el-button>
    </div>
    <el-table :data="spaceList" v-loading="loading" border style="width: 100%">
      <el-table-column prop="id" label="ID" width="80" />
      <el-table-column prop="spaceName" label="空间名称" />
      <el-table-column prop="spaceLevel" label="空间级别">
        <template #default="{ row }">
          <span>
            {{ spaceLevels.find((l) => l.value === row.spaceLevel)?.text || row.spaceLevel }}
          </span>
        </template>
      </el-table-column>
      <el-table-column label="最大容量">
        <template #default="{ row }">
          <span>
            {{ formatFileSize(row.maxSize) }}
          </span>
        </template>
      </el-table-column>
      <el-table-column prop="maxCount" label="最大数量" />
      <el-table-column label="操作" width="180">
        <template #default="{ row }">
          <el-button size="small" @click="openEditDialog(row)">编辑</el-button>
          <el-button size="small" type="danger" @click="handleDelete(row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div style="margin-top: 16px; text-align: right">
      <el-pagination
        background
        layout="prev, pager, next, jumper, total"
        :current-page="queryParams.pageNum"
        :page-size="queryParams.pageSize"
        :total="total"
        @current-change="handlePageChange"
      />
    </div>
  </el-card>

  <el-dialog
    :title="isEdit ? '编辑空间' : '新增空间'"
    v-model="dialogVisible"
    width="400px"
    @close="dialogVisible = false"
  >
    <el-form :model="form" label-width="90px">
      <el-form-item label="空间名称" required>
        <el-input v-model="form.spaceName" />
      </el-form-item>
      <el-form-item label="空间级别" required>
        <el-select v-model="form.spaceLevel" placeholder="请选择空间级别">
          <el-option
            v-for="level in spaceLevels"
            :key="level.value"
            :label="level.text"
            :value="level.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="最大容量">
        <el-input-number v-model="form.maxSize" :min="0" />
      </el-form-item>
      <el-form-item label="最大数量">
        <el-input-number v-model="form.maxCount" :min="0" />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="dialogVisible = false">取消</el-button>
      <el-button type="primary" @click="handleSubmit">确定</el-button>
    </template>
  </el-dialog>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue'
import {
  addSpaceAPI,
  deleteSpaceAPI,
  updateSpaceAPI,
  getSpaceByIdAPI,
  listSpaceByPageAPI,
  listSpaceLevelAPI,
} from '@/api/spaceAPI'
import formatFileSize from '@/utils/formatFileSize'

// 空间级别列表
const spaceLevels = ref([])

// 查询参数
const queryParams = reactive({
  pageNum: 1,
  pageSize: 10,
  spaceName: '',
  spaceLevel: null,
  sortField: '',
  sortOrder: 'ASC',
})

// 空间列表和分页
const spaceList = ref([])
const total = ref(0)
const loading = ref(false)

// 新增/编辑弹窗
const dialogVisible = ref(false)
const isEdit = ref(false)
const form = reactive({
  id: null,
  spaceName: '',
  spaceLevel: null,
  maxSize: null,
  maxCount: null,
})

// 获取空间级别
const fetchSpaceLevels = async () => {
  const res = await listSpaceLevelAPI()
  spaceLevels.value = res.data || []
}

// 获取空间列表
const fetchSpaceList = async () => {
  loading.value = true
  const res = await listSpaceByPageAPI({ ...queryParams })
  spaceList.value = res.data?.records || []
  total.value = res.data?.total || 0
  loading.value = false
}

// 打开新增弹窗
const openAddDialog = () => {
  isEdit.value = false
  Object.assign(form, { id: null, spaceName: '', spaceLevel: null, maxSize: null, maxCount: null })
  dialogVisible.value = true
}

// 打开编辑弹窗
const openEditDialog = async (row) => {
  isEdit.value = true
  const res = await getSpaceByIdAPI(row.id)
  Object.assign(form, res.data)
  dialogVisible.value = true
}

// 提交新增/编辑
const handleSubmit = async () => {
  if (isEdit.value) {
    await updateSpaceAPI(form)
  } else {
    await addSpaceAPI(form)
  }
  dialogVisible.value = false
  fetchSpaceList()
}

// 删除空间
const handleDelete = async (row) => {
  await deleteSpaceAPI(row.id)
  fetchSpaceList()
}

// 分页变化
const handlePageChange = (page) => {
  queryParams.pageNum = page
  fetchSpaceList()
}

// 查询
const handleSearch = () => {
  queryParams.pageNum = 1
  fetchSpaceList()
}

onMounted(() => {
  fetchSpaceLevels()
  fetchSpaceList()
})
</script>
